//
//  QYHomeViewController.m
//  CarPooling
//
//  Created by Dxc_iOS on 2018/5/14.
//  Copyright © 2018年 代星创. All rights reserved.
//

#import "QYHomeViewController.h"
#import <UserNotifications/UserNotifications.h>
#import "DXCCategory.h"
#import "QYShareViewController.h"
#import "LEEAlert.h"
#import "OpenPushView.h"
#import "SignView.h"
#import <SVProgressHUD.h>
#import "QYOCJSViewController.h"
#import "QYLuckyLotteryViewController.h"
#import "UIImage+GradientColor.h"
#import "UIFont+QYUIFont.h"
extern CFAbsoluteTime StartTime;
@interface QYHomeViewController ()
@property (nonatomic, copy) NSArray<NSString *> *weathers;
@end

@implementation QYHomeViewController
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    double finishTime = (CFAbsoluteTimeGetCurrent() - StartTime);
    NSLog(@"ViewdidAppear ---------> %f", finishTime);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
     self.weathers = @[@"物理", @"英语", @""];
    [self setupHomeUI];
    [self isOpenMessageNotificationService];

}
#pragma mark – Life Cycle
-(void)setupHomeUI{
//   获取当前系统支持的字体
//    for (NSString *fontFamilyName in UIFont.familyNames) {
//        NSLog(@"familyName:%@", fontFamilyName);
//        for (NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) {
//            NSLog(@"fontName:%@", fontName);
//        }
//    }
    
//    使用自定义字体 - 苹方字体
//    苹方-简 中黑体 PingFangSC-Medium
//    苹方-简 中粗体 PingFangSC-Semibold
//    苹方-简 细体 PingFangSC-Light
//    苹方-简 极细体 PingFangSC-Ultralight
//    苹方-简 常规体 PingFangSC-Regular
//    苹方-简 纤细体 PingFangSC-Thin
    // 苹方字体，默认为PingFangSC-Regular
//    UIFont *font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    UIFont *font =  [UIFont pingFangSCWithWeight:FontWeightStyleSemibold size:14];
    NSString *familyName = font.familyName;
    NSString *fontName = font.fontName;
    NSLog(@"familyName:%@\nfontName:%@", familyName, fontName);
  
    
    
    
    
    //   渐变色
    CGSize SCREEN_SIZE = CGSizeMake(self.view.frame.size.width ,self.view.frame.size.height);
    UIColor *topleftColor = [UIColor colorWithRed:48/255.0f green:127/255.0f blue:202/255.0f alpha:1.0f];
    UIColor *bottomrightColor = [UIColor colorWithRed:35/255.0f green:195/255.0f blue:95/255.0f alpha:1.0f];
    UIImage *bgImg = [UIImage gradientColorImageFromColors:@[topleftColor, bottomrightColor] gradientType:GradientTypeTopToBottom imgSize:SCREEN_SIZE];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:bgImg];

    
    
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:shareBtn];
    shareBtn.frame = CGRectMake(30, 100, 80, 50);
    [shareBtn setTitle:@"分享" forState:UIControlStateNormal];
    shareBtn.backgroundColor = [UIColor blackColor];
    [shareBtn addTarget:self action:@selector(shareBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [shareBtn qy_setCornerRadius:6];
    
    UIButton *PushBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:PushBtn];
    PushBtn.frame = CGRectMake(shareBtn.right+10,100, 80, 50);
    [PushBtn setTitle:@"推送" forState:UIControlStateNormal];
    PushBtn.backgroundColor = [UIColor blackColor];
    [PushBtn addTarget:self action:@selector(PushBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [PushBtn qy_setCornerRadius:6];
    
    UIButton *SignBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:SignBtn];
    SignBtn.frame = CGRectMake(PushBtn.right+10,100, 80, 50);
    [SignBtn setTitle:@"签到" forState:UIControlStateNormal];
    SignBtn.backgroundColor = [UIColor blackColor];
    [SignBtn addTarget:self action:@selector(SignBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [SignBtn qy_setCornerRadius:6];
    
    
    UIButton *SignBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:SignBtn2];
    SignBtn2.frame = CGRectMake(30,SignBtn.qy_bottom +50, 150, 50);
    [SignBtn2 setTitle:@"SVProgressHUD" forState:UIControlStateNormal];
    SignBtn2.backgroundColor = [UIColor blackColor];
    [SignBtn2 addTarget:self action:@selector(SignBtn2Clicked) forControlEvents:UIControlEventTouchUpInside];
    [SignBtn2 qy_setCornerRadius:6];
    
    
    UIButton *SignBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:SignBtn3];
    SignBtn3.frame = CGRectMake(30,SignBtn2.qy_bottom +50, 150, 50);
    [SignBtn3 setTitle:@"OC与JS交互" forState:UIControlStateNormal];
    SignBtn3.backgroundColor = [UIColor blackColor];
    [SignBtn3 addTarget:self action:@selector(SignBtn3Clicked) forControlEvents:UIControlEventTouchUpInside];
    [SignBtn3 qy_setCornerRadius:6];
    
    

    
    
    UIButton *SignBtn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:SignBtn4];
    SignBtn4.frame = CGRectMake(30,SignBtn3.qy_bottom +50, 150, 50);
    [SignBtn4 setTitle:@"更换app图标" forState:UIControlStateNormal];
    SignBtn4.backgroundColor = [UIColor blackColor];
    [SignBtn4 addTarget:self action:@selector(SignBtn4Clicked) forControlEvents:UIControlEventTouchUpInside];
    [SignBtn4 qy_setCornerRadius:6];
    
    
        UIButton *SignBtn5 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:SignBtn5];
        SignBtn5.frame = CGRectMake(30,SignBtn4.qy_bottom +50, 150, 50);
        [SignBtn5 setTitle:@"抽奖" forState:UIControlStateNormal];
        SignBtn5.backgroundColor = [UIColor blackColor];
        [SignBtn5 addTarget:self action:@selector(SignBtn5Clicked) forControlEvents:UIControlEventTouchUpInside];
        [SignBtn5 qy_setCornerRadius:6];
    
        SignBtn5.titleLabel.font = [UIFont pingFangSCWithWeight:FontWeightStyleThin size:15];;
    
        NSLog(@"scale = %f",[UIScreen mainScreen].scale);
  //多次点击只让最后一次操作生效
//     [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(SignBtn5Clicked) object:nil];
//    [self performSelector:@selector(SignBtn5Clicked) withObject:nil afterDelay:0.3f];
    
    
}
-(void)isOpenMessageNotificationService{
    
    if (@available(iOS 10.0, *)) {//iOS 10 以后
        [[UNUserNotificationCenter currentNotificationCenter]getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
            UNNotificationSetting notificationSetting = settings.notificationCenterSetting;
            if (notificationSetting == UNNotificationSettingEnabled) {
                NSLog(@"打开了通知");
            }else if(notificationSetting == UNNotificationSettingDisabled){
                  NSLog(@"未打开了通知");
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];

                
            }else{//UNNotificationSettingNotSupported
                
            }
        }];
    } else {
        if (@available(iOS 8.0, *)) {
            if([[UIApplication sharedApplication] currentUserNotificationSettings].types == UIUserNotificationTypeNone){
                NSLog(@"未打开了通知");
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
            }else{
                
            }
        }else{
            if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes]  == UIRemoteNotificationTypeNone){
                [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
            }else{
                
            }
        }
    }
    
    // 去设置
    
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];


}
#pragma mark - BtnAction Events



- (void)setAppIconWithName:(NSString *)iconName {
    if (![[UIApplication sharedApplication] supportsAlternateIcons]) {
        return;
    }
    
    if ([iconName isEqualToString:@""]) {
        iconName = nil;
    }
    [[UIApplication sharedApplication] setAlternateIconName:iconName completionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"更换app图标发生错误了 ： %@",error);
        }
    }];
}



-(void)SignBtn5Clicked{
    [self.navigationController pushViewController:[QYLuckyLotteryViewController new] animated:YES];
}

-(void)SignBtn4Clicked{
    NSString *weather = self.weathers[arc4random() % self.weathers.count];
    [self setAppIconWithName:weather];
}
-(void)SignBtn3Clicked{
    [self.navigationController pushViewController:[QYOCJSViewController new] animated:YES];
}
-(void)SignBtn2Clicked{
    [SVProgressHUD showWithStatus:@"加载中..."];
//    [SVProgressHUD showInfoWithStatus:@"登录成功"];
//    [SVProgressHUD showErrorWithStatus:@"登录失败"];
//    [SVProgressHUD showProgress:0.5 status:@"下载中..."];
//    [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"登录成功"];
//    [SVProgressHUD showProgress:1.0];
    

}
-(void)shareBtnClicked{
    [self.navigationController pushViewController:[QYShareViewController new] animated:YES];
}
-(void)PushBtnClicked{
    OpenPushView *view = [[OpenPushView alloc] initWithFrame:CGRectMake(0, 0, 280, 0)];
    
    view.closeBlock = ^{
        
        [LEEAlert closeWithCompletionBlock:nil];
    };
    
    [LEEAlert alert].config
    .LeeCustomView(view)
    .LeeHeaderInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeShow();
}
    
-(void)SignBtnClicked{
    SignView *view = [[SignView alloc] initWithFrame:CGRectMake(0, 0, 280, 0)];
    
    view.closeBlock = ^{
        
        [LEEAlert closeWithCompletionBlock:nil];
    };
    
    [LEEAlert alert].config
    .LeeCustomView(view)
    .LeeHeaderInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    .LeeHeaderColor([UIColor clearColor])
    .LeeShow();
}
#pragma mark – Private Methods

#pragma mark - UITextFieldDelegate

#pragma mark - UITableViewDataSource

#pragma mark - UITableViewDelegate

#pragma mark - Custom Delegates

#pragma mark – Getters and Setters




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
